package com.dragon.mapper;

import com.dragon.pojo.Problem.Problem;
import com.dragon.pojo.Problem.ProblemList;
import com.dragon.pojo.Problem.ProblemSelectInfo;
import com.dragon.pojo.Problem.SubmitTestList;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface ProblemMapper {
    Integer getProblemTotal(ProblemSelectInfo info);

    List<ProblemList> selectProblemList(ProblemSelectInfo info);

    @Select("SELECT problem.id, title_name, algorithm_tag, description, input_format, output_format, tips, difficulty, time_limit, memory_limit, input_example, output_example, creator," +
            " problem.create_time, problem.update_time,IFNULL(MAX(record.complete_status), 0) as complete_status, IFNULL(MAX(record.score), 0) as score " +
            "from problem left join record on problem.id = problem_id AND user_id=#{userId} where problem.id = #{problemId} " +
            "group by problem.id")
    Problem getProblemInfo(Integer problemId, Integer userId);

    @Update("INSERT INTO problem (title_name,problem.algorithm_tag,description,input_format,output_format,tips,difficulty,time_limit,memory_limit,input_example,output_example,input_list,output_list,creator,create_time,update_time)" +
            "values (#{titleName},#{algorithmTag},#{description},#{inputFormat},#{outputFormat},#{tips},#{difficulty},#{timeLimit},#{memoryLimit},#{inputExample},#{outputExample},#{inputList},#{outputList},#{creator},now(),now())")
    void insertProblem(Problem problem);

    @Delete("DELETE from problem where id=#{problemId}")
    void deleteProblemFromProblem(Integer problemId);

    @Update("UPDATE problem set title_name=#{titleName},algorithm_tag=#{algorithmTag},description=#{description},description=#{description},input_format=#{inputFormat},output_format=#{outputFormat}," +
            "tips=#{tips},difficulty=#{difficulty},time_limit=#{timeLimit},memory_limit=#{memoryLimit},input_example=#{inputExample},output_example=#{outputExample},input_list=#{inputList},output_list=#{outputList},update_time=now() where id=#{id}")
    void updateProblem(Problem problem);

    @Select("SELECT id, title_name, algorithm_tag, description, input_format, output_format, tips, difficulty, time_limit, memory_limit, input_example, output_example, input_list, output_list, creator," +
            " create_time, update_time from problem where id = #{problemId}")
    Problem getAllProblemInfo(Integer problemId);

    @Select("SELECT input_list, output_list,time_limit,memory_limit FROM problem where id=#{problemId}")
    SubmitTestList getTestList(Integer problemId);

    @Select("SELECT title_name from problem where id=#{problemId}")
    String getProblemTitle(Integer problemId);

    @Select("SELECT description, input_list, output_list from problem where id = #{problemId}")
    Problem getProblemBaseInfo(Integer problemId);
}
